package com.sunspring.jbug.config;

import com.sunspring.jbug.aop.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 全局web配置
 * @author wulei
 * @date 2019-03-06
 **/
@Configuration
public class GlobalWebConfig implements WebMvcConfigurer {

    static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };

    @Autowired
    private AuthInterceptor authInterceptor;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOrigins("*").
                allowCredentials(true).
                allowedMethods(ORIGINS)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/jbug/reg/verificationCode")
                .excludePathPatterns("/jbug/reg")
                .excludePathPatterns("/portal/login");
    }

}
